﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using School.Entities;
using System.Collections;
using System.IO;
using School.Admission.ReportsCls;

namespace School.Admission
{
    public class FileOrder
    {
        private static SchoolEntities db = new SchoolEntities();

        public static Int64 Add(ADM_File_Order fileorder)
        {
            db.AddToADM_File_Order(fileorder);

            db.SaveChanges();
            return fileorder.Payment_Order_Number;
        }
        public static ADM_File_Order  Single(long id)
        {
            try
            {
                return db.ADM_File_Order.Single(x => x.Payment_Order_Number == id);
            }
            catch
            {
                return null;
            }
        }


        public static IEnumerable<ADM_File_Order> All(Hashtable items)
        {
            SchoolEntities db = new SchoolEntities();

            var query = from fileorder in db.ADM_File_Order select fileorder;
            foreach (DictionaryEntry item in items)
            {
                if (item.Key == "Payment_Order_Number")
                {
                    long payNo;
                    payNo= Convert.ToInt64(item.Value); 
                    query = query.Where(c => c.Payment_Order_Number == payNo );
                }
                if (item.Key == "FirstName")
                {
                    query = query.Where(c => c.FirstName == item.Value);
                }
                if (item.Key == "Date")
                {
                    DateTime myDate;
                    myDate = Convert.ToDateTime(item.Value);
                    query = query.Where(c => c.Date  == myDate );
                }
            }
            return query.ToList();
        }

        public static void Update(ADM_File_Order fileorder)
        {

            ADM_File_Order c = db.ADM_File_Order.Single(x => x.Payment_Order_Number == fileorder.Payment_Order_Number);
            c.Payment_Order_Number = fileorder.Payment_Order_Number;
            c.Price = fileorder.Price;
            
            c.FirstName = fileorder.FirstName;
            c.SecondName = fileorder.SecondName;
            c.ThirdName = fileorder.ThirdName;
            c.FourthName = fileorder.FourthName;
            c.Date = fileorder.Date;
            db.SaveChanges();

        }

        public static Boolean  Delete(long id)
        {
            try
            {
                db.ADM_File_Order.DeleteObject(db.ADM_File_Order.Single(x => x.Payment_Order_Number  == id));
                db.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }
        public static long Expen_ID(string screen)
        {

            var expenses = from x in db.ACC_Expenses_Screens where x.Screen_Id == (from y in db.SEC_Screen where y.Code == screen select y.Id).FirstOrDefault() select x;
            long e = expenses.FirstOrDefault().Expense_Id;
            return e;
        }
        public static decimal Expense_Value(long Expense_Id)
        {

            var expenses = from x in db.ACC_Expense_Payments 
                           where x.Expense_Id == Expense_Id 
                           select x;
            decimal  e = expenses.FirstOrDefault().Value ;
            return e;
        }
        public IQueryable<rptFileOrdercls> GetFileOrder(int Id)
        {
            var query = from file in db.ADM_File_Order
                          where file.Payment_Order_Number == Id
                          select new ReportsCls.rptFileOrdercls
                          {
                              Id = file.Payment_Order_Number,
                              studentFullName = file.FirstName + " " + file.SecondName + " " + file.ThirdName + " " + file.FourthName ,
                              Date = file.Date,
                              Campus  = file.ORG_Campus.Name ,
                              Certificate= file.ORG_Certificate.Name,
                              Stage=file.ORG_Stage.Name,
                              Grade=file.ORG_Grade.Name,
                              Price=file.Price
                          };

            return query;

        }

    }
}
